﻿using UnityEngine;
using System.Collections;

public class TempCharacterSelectWindow : LTEventListenerC {
	public GameObject[] characterButtonList;
	[HideInInspector]
	public MainSceneManager manager;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnEnable()
	{
		LoadingWindow.window.ShowShortLoading();
		if( MyInfo.Instance.MyCharacterList.Count == 0 )
			RestAPI.singleton.GetMyCharacterList(RestCallback);
		else
			SetCharacterList();
	}

	void SetCharacterList()
	{
		LoadingWindow.window.HideLoading();
		for(int i=0; i<characterButtonList.Length; i++)
		{
			if( MyInfo.Instance.MyCharacterList.Contains(i+1) )
			{
				if( i == MyInfo.Instance.pawnId - 1)
				{
					characterButtonList[i].GetComponent<UISprite>().spriteName = "UI_btn01";
				}
				else
					characterButtonList[i].GetComponent<UISprite>().spriteName = "UI_btn_red";
				characterButtonList[i].SetActive(true);
			}
		}
        characterButtonList[5].SetActive(true);
	}

	int _selectedCharacterId = -1;
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);

		_selectedCharacterId = eventID;
		LoadingWindow.window.ShowShortLoading();
		RestAPI.singleton.SetCurrentCharacter(RestCallback, eventID);
	}

	public void RestCallback(Hashtable returnData)
	{
		switch( (RestAPIProtocol)returnData["protocol"] )
		{
		case RestAPIProtocol.GETMYCHARACTERLIST:
			SetCharacterList();
			break;
		case RestAPIProtocol.SETCURRENTCHARACTER:
            //manager.SetCharacterObject(_selectedCharacterId);
			LoadingWindow.window.HideLoading();
			gameObject.SetActive(false);
			break;
		}
	}
}
